<?
/**
 * VoiceOne Settings/Rules/outgoing/edit
 *
 * Frontend to provide access to the rules system in voiceone.
 * @package voiceone
 * @subpackage  settings_rules
 */
  include ("../../../../admin/config/cfg.php");
  global $cwlang, $cwLanguage;
  require_once("$absBasePath/lib/xajax/xajax.inc.php"); 
  
  //================ XAJAX ================
	function roll(){
    global $relBasePath,$cwlang;
    // Instantiate the xajaxResponse object
    $objResponse = new xajaxResponse();
    // Build response
    $html=" <img src=\"".$relBasePath."public/img/ico-progress.gif"."\" alt=\"loading\" /><span>".T_("loading")."</span>";
    $objResponse->addAssign("loading","innerHTML",$html);
    // Return the xajaxResponse object
    return $objResponse->getXML(); 
  }
  function processFormData($formData){
    global $asterisk,$cwlang,$relBasePath;
    // Instantiate the xajaxResponse object
    $objResponse = new xajaxResponse();
    // Extract form's params
    $id=$formData["id"];
    $name=$formData["name"];
    $imgErr="<img src=\"".$relBasePath."/public/img/shutdown.png\" alt=\"error\">&nbsp;&nbsp;";
    // Retrieve all ruleset
    $rulesetArr=$asterisk->getRule(0,RULE_OUTGOING);
    if(is_array($rulesetArr)){
      foreach($rulesetArr as $rs) {
        if($id==$rs["id"]) continue;
        $rsNameArr[]=$rs["name"];
      }
    }
    // Check Name ((not null)&&(unique))
    $nameErr=false;
    if($name=="") { 
      //name cannot be null
      $errOut[]= T_("Name cannot be null");
      $objResponse->addAssign("nameErr","innerHTML",$imgErr);
      $nameErr=true;  
    }else if((is_array($rsNameArr))&&(in_array($name,$rsNameArr))){
      //name must be unique
      $errOut[]= T_("Name already exists. Please change it");
      $objResponse->addAssign("nameErr","innerHTML",$imgErr);
      $nameErr=true;
    }else{
      //name must be alphanumeric
      for($i=0; $i<strlen($name); $i++){
        $ch=ord($name[$i]);
        if(!((($ch>96)&&($ch<123))||(($ch>64)&&($ch<91))||(($ch>47)&&($ch<58))||($ch==95)||($ch==45))){
          $errOut[]=sprintf(T_("Rule set name doesn't allow character \"%s\" Please change it"),$extension ,$surname, $name );
          $objResponse->addAssign("nameErr","innerHTML",$imgErr);
          $nameErr=true;
          break;
        }
      }
    }
    if($nameErr===false) $objResponse->addAssign("nameErr","innerHTML","");
    // Process Form
    if(isset($errOut)){
      $error="<div class=\"AlertBad\"><ul>";
      foreach($errOut as $err) $error.="<li>".$err."</li>";
      $error.="</ul></div>";
      $objResponse->addAssign("alert","innerHTML",$error);
      $objResponse->addAssign("loading","innerHTML","");
    }else{
      $objResponse->addAssign("loading","innerHTML",""); 
      $objResponse->addScript(" document.getElementById('mainform').submit(); ");
    }
    // Return the xajaxResponse object
    return $objResponse->getXML();  
  }
  $xajax = new xajax();
  $xajax->registerFunction("roll");
  $xajax->registerFunction("processFormData");
  $xajax->processRequests();
  
  //==================== POST ====================
  if (count($_POST)>0) {
    $entry=Array(
      "id" => $_POST["id"],
      "name" => $_POST["name"],
      "description" => $_POST["description"],
      "type" => RULE_OUTGOING 
    );
    //save the ruleset informations
    $ret=$asterisk->putRule($entry);
    if ($ret>0) {
      $alert="<div class=\"AlertGood\">".T_("Rule set created")."</div>";
      $_GET["id"]=$ret;
    } else $alert="<div class=\"AlertBad\">".T_("Error: Rule set not created")."</div>";
    //return to main page
    goToMainPage($alert);
  }
  
  //================ PAGE LOGIC ================
  //if modify, read the ruleset informations
  if (isset($_GET["id"])) {
    $ret=$asterisk->getRule($_REQUEST["id"],RULE_OUTGOING);
    if ($err=$asterisk->getError()) die("Error: $err");
    $id=$ret[0]["id"];
    $name=$ret[0]["name"];
    $description=$ret[0]["description"]; 
    // the name can't be changed, because of it is written all over the asterisk
    $disableName=" disabled=\"disabled\" ";
  }  
  
  //================ MAIN CONTENTS ================
  ob_start();
  $xajax->printJavascript("../../../../admin/lib/xajax/"); 
  ?>
  <?$sel="outgoing";include("../../navigation.php");?>
  
  <div class="SectionHeader">
    <div id="loading" style="float: right;"></div>
    <h1><?=T_("Outgoing rule set")?></h1>
    <h3><a href="../" class="admin" ><?=T_("Return without save")?></a></h3>
  </div>
  <div id="alert"></div>
  <div id="basic_form">
    <form id="mainform" action="<?=$PHP_SELF?>" method="post" onsubmit="document.getElementById('alert').innerHTML=''; xajax_processFormData(xajax.getFormValues('mainform')); return false;" >
      <p class="blockintro"></p>
      <div class="block">
        <table>
          <tr>
            <td class="required"><label for="name"><?=T_("Name")?>:</label></td>
            <td><input type="text" size="25" maxlength="30" id="name" name="name" value="<?=$name?>" <?=$disableName?>/><span id="nameErr"></span></td>
          </tr>
          <tr>
            <td class="required"><label for="description"><?=T_("Description")?>:</label></td>
            <td><textarea rows="10" cols="40" id="description" name="description"><?=$description?></textarea></td>
          </tr>
        </table>
      </div>
      <div class="action">
        <?if (isset($_GET["id"])) {?><input type="hidden" name="id" value="<?=$id?>" /><input type="hidden" name="name" value="<?=$name?>" /><?}?>
        <input type="image" align="right" src="<?=$relBasePath?>/public/img/<?=$cwLanguage?>/b-save.gif" alt="Save"  />
        <img style="cursor: pointer;" vspace="2" src="<?=$relBasePath?>/public/img/<?=$cwLanguage?>/b-cancel.gif" alt="Cancel" onclick="location.href='../'; return false;" />
     <!--  
        <input type="image" src="<?=$relBasePath?>/public/img/<?=$cwLanguage?>/b-cancel.gif" alt="Cancel" onclick="location.href='../'; return false;"/>
        <input type="image" src="<?=$relBasePath?>/public/img/<?=$cwLanguage?>/b-save.gif" alt="Save" />
      -->
      </div>
    </form>
  </div>  
  
  <?
  $contents=ob_get_contents();
  ob_end_clean();

  //================ SIDEBAR ================
  ob_start(); 
  ?>
    <div class="Sidebar">
      <h1><?=T_("Edit the Rule Set")?></h1>
      <p><?=T_("To be done...")?></p>
    </div>
  <?  
  $sidebar=ob_get_contents();
  ob_end_clean();

  //================ SETTINGS ================

  $smarty->assign("section", "rules");
  $smarty->assign("subsection", "");
  $smarty->assign("templateType", "TwoColLeft");  //OneCol, TwoColLeft, TwoColRight
  $smarty->assign("contents", $contents);
  $smarty->assign("sidebar", $sidebar);
  $smarty->display('settings.tpl');
?>

